package it.unical.canonaco.rende.igpe.game.cbv.datastructure;

import java.util.LinkedList;
import java.util.List;
/** 
 * @author Fabio Rende*/
public class TreeNode<T> {

    private LinkedList<TreeNode<T>> children;
    private TreeNode<T> parent;
    private T value;

    public TreeNode<T> getParent() {
        return this.parent;
    }

    public List<TreeNode<T>> getChildren() {
        return this.children;
    }

    public int getNumberOfChildren() {
    	if(children != null)
    		return getChildren().size();
    	return 0;
    }

    public boolean hasChildren() {
        return (getNumberOfChildren() > 0);
    }

    public void setChildren(LinkedList<TreeNode<T>> children) {
        for(TreeNode<T> child : children) {
           child.parent = this;
        }

        this.children = children;
    }

    public void addChild(TreeNode<T> child) {
    	if(children == null)
    		children = new LinkedList<>();
       
    		child.parent = this;
//    		System.out.println(parent.getValue());
        children.addLast(child);
    }
    
    public TreeNode<T> getFirstChild()
    {
    	if(children != null && !children.isEmpty())
    		return children.get(0);
    	
    	System.err.println("a null child");
    	return null;
    }
    public TreeNode<T> removeFirstChild()
    {
    	if(children != null)
    		return children.pop();
    	System.out.println("non ha figli e ritorno null");
    	return null;
//    		children.removeFirst();
    }
    public void removeChildren() {
        this.children = new LinkedList<TreeNode<T>>();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
           return true;
        }
        if (obj == null) {
           return false;
        }
        if (getClass() != obj.getClass()) {
           return false;
        }
        TreeNode other = (TreeNode) obj;
      
        return true;
    }

	/**
	 * @return the value
	 */
	public T getValue() {
		return value;
	}

	/**
	 * @param value the value to set
	 */
	public void setValue(T value) {
		this.value = value;
	}
}


